package com.lhd.tool;

import com.lhd.bean.Colinfo;
import lombok.extern.slf4j.Slf4j;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

/**
 * 类 {@code Tool} 写入文件攻击.
 *
 * <p>
 * 将表的一行测试数据写入文件
 *
 * @author MapLover
 * @since 2020.09
 */
@Slf4j
public class Tool2 {

    private static NumberTool genNum = new NumberTool();
    private static StringTool genStr = new StringTool();
    private static DateTool genDate = new DateTool();

    private final BufferedWriter bwriter;

    private static List<Colinfo> clist;

    public Tool2(List<Colinfo> clist, String fileName) throws IOException {
        Tool2.clist = clist;
        this.bwriter = new BufferedWriter(new FileWriter(new File(fileName), true));
    }

    private synchronized static String GenFiledByType(Colinfo c) {
        String line = "";
        //log.info(c.getDataType());
        //System.out.println(c.getDataType());

        switch (c.getDataType()) {
            case "VARCHAR":
            case "VARCHAR2":
            case "CHAR":


            case "CHAR2":
                line += genStr.getRandomString(c.getDataLength()) + ",";
                break;

            case "DATE":
                line += genDate.getRandomDate() + ",";
                break;

            case "NUMBER":
                line += genNum.getRandomAmt(c.getDataPrecision(), c.getDataScale()) + ",";
                break;

            default:
                line += "8"+ ",";
                //line += genNum.getRandomNumber(1) + ",";

        }
        return line;

    }

    public synchronized static String GenRowByRule(List<Colinfo> clist, int ti) {
        String line = "";

        for (int i = 0; i < clist.size(); i++) {

            Colinfo c = (Colinfo) clist.get(i);
            //log.info(c.getRule());
            //System.out.print(c.getRule()+">>>>>>>>>>>");

            switch (c.getRule()) {

                case "ID":
                    line += genStr.lpad(String.valueOf(ti), 2, "0") + ",";
                    continue;
                case "NAME":
                    line += genStr.getRandomString(c.getDataLength()) + ",";
                    continue;
                case "SEQ":
                    line += genStr.lpad(String.valueOf(ti), 2, "0") + genStr.getSeqnumeric(c.getDataLength() - 2) + ",";
                    continue;
                case "DATE":
                    line += genDate.getRandomDate() + ",";
                    continue;
                case "TIME":
                    line += genDate.getRandomTime() + ",";
                    continue;
                case "AMT":
                    line += genNum.getRandomAmt(c.getDataPrecision(), c.getDataScale()) + ",";
                    continue;
                case "BIRTH":
                    line += genNum.getGaussRandomNumber(45) + ",";
                    continue;
                case "RAT":
                    line += genNum.getRandomRate(c.getDataScale()) + ",";
                    continue;
                case "CCY"://币种
                    line += "01,";// 默认RMB
                    continue;
                case "TXN"://交易流水
                    line += genStr.getRandomNumeric(c.getDataLength()) + ",";
                    continue;
                case "CODE":
                    line += genStr.getRandomBigString(c.getDataLength())+ ",";
                    continue;
                default:
                    line += GenFiledByType(c);
                    continue;
            }
        }
        // System.out.print(line);
        line = line.substring(0, line.lastIndexOf(","));

        return line;
    }

    /**
     * @param fileName  待写入文件名
     * @param threadNum 写入并发数(线程数)
     * @param threadNo  第几个线程
     * @param totalRows 该表要生成的行数
     */
    public synchronized void SaveAsBufferWriter(String fileName, int threadNum, int threadNo, int totalRows) {
        try {
            int avgRows = totalRows / threadNum;
            for (int i = 1; i <= avgRows; i++) {
                String line = Tool2.GenRowByRule(clist, threadNo) + "\n";

                bwriter.write(line);
                bwriter.flush();

                if (i % 5000 == 0) {
                    log.info(Thread.currentThread().getName() + " Write " + i + " rows!");
                }

            }
            bwriter.close();
            log.info(Thread.currentThread().getName() + " ------------------------closed");
        } catch (IOException e) {
            log.error("text");
            log.error(e.getMessage());
        }
    }
}
